<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------

namespace Common\Model;
use Think\Model;

class ScoreModel extends Model
{
    protected $tableName="Score_out";
    public $score=[
        "1"=>1,     //借阅评论
        "2"=>2,      //购书评论
        "3"=>3,      //视频评论
        "4"=>5       //参与活动
    ];
    //增加积分
    function addscore($user_id,$type, $number=null)
    {
        $data = [
            'user_id' => $user_id,
            'type' => $type,
            'score' => $this->scoreValue($type,$number),
        ];
        $this->startTrans();
        $out_id=$this->data($data)->add();
        if($out_id){
            $this->commit();
            return $data['score'];
        }else{
            $this->rollback();
            return 0;
        }
    }
    //积分兑换-兑换1
    public function exchange($user_id,$score)
    {
        $data = [
            'user_id' => $user_id,
            'type' => '-1',
            'score' => - $score,
        ];
        $this->add($data);
    }
    // 获取分值
    public function scoreValue($type, $number = null)
    {
        $list = [
            '1' => config('borrow_comment_score'),
            '2' => config('buy_comment_score'),
            '3' => config('video_score'),
            '4' => config('apply_score'),
            '5' => config('buy_book'),
            '6' => config('borrow_book')
        ];
        if ($number) {
            return $list[$type] * $number;
        }
        return $list[$type];
    }

    public function total($user_id)
    {
        $d=$this->where("user_id=$user_id")->sum('score');
        return $d;
    }


}